// 单例模式 - 课程管理器
export class CourseManager {
  static instance = null
  
  constructor() {
    this.courses = [
      { id: 1, name: '数据结构', code: 'CS101', semester: '2022-2023-1', teacher: '张教授', experimentCount: 5 },
      { id: 2, name: '操作系统', code: 'CS102', semester: '2022-2023-2', teacher: '李教授', experimentCount: 8 },
    ]
  }
  
  static getInstance() {
    if (!this.instance) {
      this.instance = new CourseManager()
    }
    return this.instance
  }
  
  getCourses() {
    return this.courses
  }
  
  getCourseById(id) {
    return this.courses.find(course => course.id === id) || null
  }
  
  addCourse(course) {
    const newId = this.courses.length > 0 ? Math.max(...this.courses.map(c => c.id)) + 1 : 1
    const newCourse = { ...course, id: newId }
    this.courses.push(newCourse)
    return newCourse
  }
  
  updateCourse(course) {
    const index = this.courses.findIndex(c => c.id === course.id)
    if (index !== -1) {
      this.courses[index] = { ...course }
      return true
    }
    return false
  }
  
  deleteCourse(id) {
    const index = this.courses.findIndex(c => c.id === id)
    if (index !== -1) {
      this.courses.splice(index, 1)
      return true
    }
    return false
  }
} 